import { Config } from "../../Config"
import { BigEvolution, BigState } from "../core/Big"
import { Player } from "../core/Player"
import Utils from "../GameUtils"
import { 拳师 } from "../evolution/拳师"
import { BaseHandler } from "./BaseHandler"

export class 拳师Handler extends BaseHandler{

    /**
     * 攻击时行动力消耗-1，相当于移动
     */
    拳师_移动(data:any,player:Player) {
        this.handlerMgr.spendAction(player,Config.COST_拳师_移动)
        let startPoint = this.gameMgr.map_point.get(data.start)
        let endPoint = this.gameMgr.map_point.get(data.end)
        let big_start = startPoint.big
        big_start.actionOnce()
        let big_end = endPoint.big
        if(big_end != null) {
            big_end.deleteState(BigState.HIDE)
            big_start.die(player,endPoint.id)
            return
        }
        //没有大子，就是移动
        startPoint.big = null
        endPoint.big = big_start
        big_start.point = endPoint
        this.gameMgr.sendBroadcast(Utils.getObj("big_move",{"start":data.start,"end":data.end}))
        big_start.terrainAdapt()
    }

    /**
     * 攻击成功后周围一圈大子和父系大子获得怯战
     */
    拳师_怒威神拳(data:any,player:Player) {
        this.handlerMgr.spendAction(player,Config.COST_拳师_怒威神拳)
        let startPoint = this.gameMgr.map_point.get(data.start)
        let endPoint = this.gameMgr.map_point.get(data.end)
        let big_start = startPoint.big
        big_start.actionOnce()
        let small = endPoint.small
        let big_end = endPoint.big
        if(small != null) {
            //小子被攻击
            small.die(player)
        } else if(big_end != null) {
            let big_end_father = big_end.father
            //大子被攻击
            if(big_end.attacked(player)){
                let qs = (big_start as 拳师)
                //打过了
                if (big_end_father != null) {
                    big_end_father.addState(BigState.FEAR)
                    qs.addMark(big_end_father)
                } else {
                    qs.removeMark()
                }
            } else {
                //攻击没打过，自己死亡
                big_start.die(player,endPoint.id)
                return
            }
        }
        //没有大子，就是移动
        startPoint.big = null
        endPoint.big = big_start
        big_start.point = endPoint
        this.gameMgr.sendBroadcast(Utils.getObj("拳师_怒威神拳",{"start":data.start,"end":data.end}))
        big_start.terrainAdapt()
    }

    /**
     * AOE伤害
     */
    拳师_爆裂火拳(data:any,player:Player) {
        this.handlerMgr.spendAction(player,Config.COST_拳师_爆裂火拳)
        let startPoint = this.gameMgr.map_point.get(data.start)
        let endPoint = this.gameMgr.map_point.get(data.end)
        let big_start = startPoint.big
        big_start.actionOnce()
        let small = endPoint.small
        let big_end = endPoint.big
        if(small != null) {
            //小子被攻击
            small.die(player)
        } else if(big_end != null) {
            //大子被攻击
            if(big_end.attacked(player)){
               //打过了
            } else {
                //攻击没打过，自己死亡
                big_start.die(player,endPoint.id)
                return
            }
        }
        let list = Utils.getArrPointsAround(this.gameMgr,endPoint)
        for (let p of list) {
            let b = p.big
            if (b != null && b.player != player) {
                b.attacked(player)
            }
        }
        //没有大子，就是移动
        startPoint.big = null
        endPoint.big = big_start
        big_start.point = endPoint
        this.gameMgr.sendBroadcast(Utils.getObj("拳师_爆裂火拳",{"start":data.start,"end":data.end}))
        big_start.terrainAdapt()
    }
}